﻿#-*- coding: utf-8 -*-
from django import template
from models.main import App_user, Post, Comment, Tag, Sidebar

from django.template import Node, NodeList, Template, Context, Variable
register = template.Library()

class Sidebar_c(template.Node):
    def __init__(self, nodelist_loop):
        self.nodelist_loop = nodelist_loop

    def render(self, context):
        nodelist = NodeList()
        sidebar_list = Sidebar.all().order('order').fetch(100)
        
        context.push()
        for sidebar in sidebar_list:
            context.push()
            context['sidebar'] = sidebar

            for node in self.nodelist_loop:
                nodelist.append(node.render(context))

            context.pop()
        context.pop()

        return nodelist.render(context)

@register.tag(name="sidebar_list")
def do_sidebar(parser, token):
    """
    {% sidebar_list %}
    """

    nodelist_loop = parser.parse(('endsidebar_list',))
    parser.delete_first_token()

    return Sidebar_c(nodelist_loop)


class Space_line_lessNode(Node):
    def __init__(self, nodelist):
        self.nodelist = nodelist

    def render(self, context):
        from django.utils.html import strip_spaces_between_tags
        html =  strip_spaces_between_tags(self.nodelist.render(context).strip())
        html = html.replace('\r\n', '').replace('\n', '')
        return html

@register.tag(name="space_line_less")
def space_line_less(parser, token):
    nodelist = parser.parse(('endspace_line_less',))
    parser.delete_first_token()
    return Space_line_lessNode(nodelist)
